package com.qiang.exception;

import com.mysql.jdbc.exceptions.MySQLSyntaxErrorException;
import com.qiang.enums.ExceptionCode;
import com.qiang.util.ResultEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.sql.SQLException;

@RestControllerAdvice
public class AppExceptionHandler implements Serializable {
	private static final Logger log = LogManager.getLogger(AppExceptionHandler.class);
	/**
	 * 自定义异常处理
	 * @param e
	 * @return
	 * @throws Exception
	 */
	@ExceptionHandler(value = ResultException.class)
	public ResultEntity exceptionHandler(HttpServletRequest req,ResultException e,HttpServletResponse response) throws Exception {
		return buildErrorInfo(e.getCode(), e.getMessage(), req,response);
	}

	@ExceptionHandler(value = NoHandlerFoundException.class)
	public ResultEntity noHandlerFoundException(HttpServletRequest req,ResultException e,HttpServletResponse response) throws Exception {
		return buildErrorInfo(ExceptionCode.PATH_ERROR.getCode(), ExceptionCode.PATH_ERROR.getMessage(), req,response);
	}

	/**
	 * 缺失参数处理
	 * @param e
	 * @param request
	 * @param response
	 * @return
	 */
	@ExceptionHandler(value = MissingServletRequestParameterException.class)
    public ResultEntity handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        return buildErrorInfo(ExceptionCode.ILLEGAL_PARAM.getCode(),
				ExceptionCode.ILLEGAL_PARAM.getMessage() +
						((MissingServletRequestParameterException) e).getParameterName(), request,response);
    }

	/**
	 * SQLException
	 * @param e
	 * @param request
	 * @return
	 */
	@ExceptionHandler(value = SQLException.class)
	public ResultEntity handleSQLException(Exception e, HttpServletRequest request,HttpServletResponse response) {
		log.error(ExceptionCode.SQL_EXCEPTION.getMessage(), e);
		return buildErrorInfo(ExceptionCode.SQL_EXCEPTION.getCode(),ExceptionCode.SQL_EXCEPTION.getMessage(),request,response);
	}

	@ExceptionHandler(value = MySQLSyntaxErrorException.class)
	public ResultEntity handleMySQLSyntaxErrorException(Exception e, HttpServletRequest request,HttpServletResponse response) {
		log.error(ExceptionCode.SQL_EXCEPTION.getMessage(), e);
		return buildErrorInfo(ExceptionCode.SQL_EXCEPTION.getCode(),ExceptionCode.SQL_EXCEPTION.getMessage(),request,response);
	}

	/**
	 * 异常组装
	 * @param code
	 * @param message
	 * @param req
	 * @return
	 */
	private ResultEntity buildErrorInfo(Integer code, String message, HttpServletRequest req,HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		ResultEntity info = new ResultEntity(code, message);
		return info;
	}
}
